/*
 * fixedpoint.h
 *
 *  Created on: 18.05.2011
 *      Author: Thomas Opolka
 *     Version: 0.5
 */

#ifndef FIXEDPOINT_H_
#define FIXEDPOINT_H_

#include <stdint.h>


typedef int16_t fixedpoint_t;  /* fixed point representation*/

#define FIXEDPOINT_FBITS 8  /* fractional bits */

/* conversion */
#define fixedpoint_fromFloat(arg)  (fixedpoint_t)(arg * (1 << FIXEDPOINT_FBITS) + (arg >= 0 ? 0.5 : -0.5))
#define fixedpoint_fromInt(arg)    (fixedpoint_t)(arg << FIXEDPOINT_FBITS)
#define fixedpoint_toInt(arg)      (int16_t)(arg >> FIXEDPOINT_FBITS)

/* calculation */
#define fixedpoint_mul(arg1, arg2) (fixedpoint_t)(((int32_t)arg1 * (int32_t)arg2) >> FIXEDPOINT_FBITS)
#define fixedpoint_div(arg1, arg2) (fixedpoint_t)(((int32_t)(arg1 << FIXEDPOINT_FBITS)) / (int32_t)arg2)


#endif /* FIXEDPOINT_H_ */
